package main

/*func invitePacket(write http.ResponseWriter, request *http.Request) {
	utils.Info("invitePacket check")

	body, err := ioutil.ReadAll(request.Body)
	defer request.Body.Close()
	utils.CheckErr(write, err, 100, "invitePacket:没有收到解密的数据")
	if err != nil {
		return
	}
	friendD := Friend{}
	err1 := json.Unmarshal(body, &friendD)
	utils.CheckErr(write, err1, 101, "invitePacket:无法解析解密的json数据")
	if err1 != nil {
		return
	}

	if friendD.ShareCode == "" {
		utils.CheckErr(write, errors.New("用户信息不能为空"), 102, "invitePacket:用户信息不能为空")
		return
	}

	if friendD.OpenId == "" {
		utils.CheckErr(write, errors.New("好友信息不能为空"), 103, "invitePacket:好友信息不能为空")
		return
	}

	// 获取好友用户信息
	friendInfo, err4 := model.UserByUniqueId(friendD.OpenId)
	utils.CheckErr(write, err4, 104, "invitePacket:获取好友用户信息失败")
	if err4 != nil {
		return
	}

	// 判断是否为新玩家
	var now = int(time.Now().Unix())
	if (now - friendInfo.Time) > 3 {
		returnDa := utils.ReturnData{
			Errcode: 0,
			Errmsg:  "is ok",
		}
		returnDa.Returnfunc(write)
		return
	}

	// 从池里获取连接
	rc := model.RedisClient.Get()
	// 用完后将连接放回连接池
	defer rc.Close()

	// 根据user_shareCode查询宿主userId
	rootUserId, err5 := redis.Int(rc.Do("HGET", utils.Config.PkgName+"-shareCode_list", friendD.ShareCode))
	utils.CheckErr(write, err5, 105, "invitePacket:查询shareCode用户id失败")
	if err5 != nil {
		return
	}

	userInfo, err6 := model.UserById(rootUserId)
	utils.CheckErr(write, err6, 106, "invitePacket:获取用户信息失败")
	if err6 != nil {
		return
	}

	if userInfo.UniqueId == friendD.OpenId {
		utils.CheckErr(write, errors.New("自己不能邀请自己"), 107, "invitePacket:自己不能添加自己为好友")
		return
	}

	// 添加action
	inviteInfo := model.InvitePacket{OpenId: userInfo.UniqueId, Invite: friendInfo.UniqueId, Amount: 30, Time: now}
	err8 := model.InvitePacketCreate(&inviteInfo)
	// 如果有错误记录
	if err8 != nil {
		utils.Error(err8)
	}

	returnDa := utils.ReturnData{
		Errcode: 0,
		Errmsg:  "is ok",
	}
	returnDa.Returnfunc(write)
}

func inviteEight(write http.ResponseWriter, request *http.Request) {
	utils.Info("invitePacket check")

	body, err := ioutil.ReadAll(request.Body)
	defer request.Body.Close()
	utils.CheckErr(write, err, 100, "inviteEight:没有收到解密的数据")
	if err != nil {
		return
	}
	friendD := Friend{}
	err1 := json.Unmarshal(body, &friendD)
	utils.CheckErr(write, err1, 101, "inviteEight:无法解析解密的json数据")
	if err1 != nil {
		return
	}

	if friendD.ShareCode == "" {
		utils.CheckErr(write, errors.New("用户信息不能为空"), 102, "inviteEight:用户信息不能为空")
		return
	}

	if friendD.OpenId == "" {
		utils.CheckErr(write, errors.New("好友信息不能为空"), 103, "inviteEight:好友信息不能为空")
		return
	}

	// 从池里获取连接
	rc := model.RedisClient.Get()
	// 用完后将连接放回连接池
	defer rc.Close()

	// 根据user_shareCode查询宿主userId
	rootUserId, err5 := redis.Int(rc.Do("HGET", utils.Config.PkgName+"-shareCode_list", friendD.ShareCode))
	utils.CheckErr(write, err5, 105, "inviteEight:查询shareCode用户id失败")
	if err5 != nil {
		return
	}

	userInfo, err6 := model.UserById(rootUserId)
	utils.CheckErr(write, err6, 106, "inviteEight:获取用户信息失败")
	if err6 != nil {
		return
	}

	if userInfo.UniqueId == friendD.OpenId {
		utils.CheckErr(write, errors.New("自己不能邀请自己"), 107, "inviteEight:自己不能邀请自己")
		return
	}

	if userInfo.Eight != 0 {
		utils.CheckErr(write, errors.New("格子已经开启"), 108, "inviteEight:格子已经开启")
		return
	}

	now := int(time.Now().Unix())

	err9 := userInfo.Update("eight", strconv.Itoa(now+24*60*60))
	utils.CheckErr(write, err9, 109, "inviteEight:设置时间失败")
	if err9 != nil {
		return
	}

	returnDa := utils.ReturnData{
		Errcode: 0,
		Errmsg:  "is ok",
	}
	returnDa.Returnfunc(write)
}

func invitePacketList(write http.ResponseWriter, request *http.Request) {

	utils.Info("invitePacketList check")

	body, err := ioutil.ReadAll(request.Body)
	defer request.Body.Close()
	utils.CheckErr(write, err, 100, "invitePacketList:没有收到解密的数据")
	if err != nil {
		return
	}
	inviteD := videoD{}
	err1 := json.Unmarshal(body, &inviteD)
	utils.CheckErr(write, err1, 101, "invitePacketList:无法解析解密的json数据")
	if err1 != nil {
		return
	}

	// 查出所有红包列表
	rows, err2 := model.Db.Query("select id, open_id, invite_id, amount, get_time, time from invite_packet where open_id = ? order by time desc", inviteD.OpenId)
	if err2 != nil { // 没有数据
		returnDa := utils.ReturnData{
			Errcode: 0,
			Errmsg:  "is ok",
			Data:    Map{},
		}
		returnDa.Returnfunc(write)
		return
	}

	returnD := make(map[int]interface{})
	var i = 0
	var headImg = "http://h5static.ledongtian.com/games/wegameresource/mahjong/noname.png"
	var name = "未授权"
	for rows.Next() {
		redPacketRow := model.InvitePacket{}
		err3 := rows.Scan(&redPacketRow.Id, &redPacketRow.OpenId, &redPacketRow.Invite, &redPacketRow.Amount, &redPacketRow.GetTime, &redPacketRow.Time)
		utils.CheckErr(write, err3, 103, "invitePacketList:查询红包列表失败")
		if err3 != nil {
			return
		}

		// 查询好友头像,nickname
		user, err4 := model.UserByUniqueId(redPacketRow.Invite)
		utils.CheckErr(write, err4, 104, "invitePacketList:查询用户信息失败")
		if err4 != nil {
			return
		}

		if user.HeadImg != "" {
			headImg = user.HeadImg
			name = user.Name
		}

		returnD[i] = Map{
			"id":       redPacketRow.Id,
			"get_time": redPacketRow.GetTime,
			"time":     redPacketRow.Time,
			"head_img": headImg,
			"name":     name,
			"amount":   redPacketRow.Amount,
		}
		i++
	}

	returnDa := utils.ReturnData{
		Errcode: 0,
		Errmsg:  "is ok",
		Data:    returnD,
	}
	returnDa.Returnfunc(write)
}

func invitePacketGet(write http.ResponseWriter, request *http.Request) {

	utils.Info("invitePacketGet check")

	body, err := ioutil.ReadAll(request.Body)
	defer request.Body.Close()
	utils.CheckErr(write, err, 100, "invitePacketGet:没有收到解密的数据")
	if err != nil {
		return
	}
	inviteD := model.PowerGet{}
	err1 := json.Unmarshal(body, &inviteD)
	utils.CheckErr(write, err1, 101, "invitePacketGet:无法解析解密的json数据")
	if err1 != nil {
		return
	}

	// 获取邀请数据
	inviteTodayInfo, err2 := model.InvitePacketGetById(inviteD.Id)
	utils.CheckErr(write, err2, 102, "invitePacketGet:邀请记录不存在")
	if err2 != nil {
		return
	}

	if inviteTodayInfo.OpenId != inviteD.OpenId {
		utils.CheckErr(write, errors.New("红包主人不合法"), 111, "invitePacketGet:红包主人不合法")
		return
	}

	if inviteTodayInfo.GetTime > 0 {
		utils.CheckErr(write, errors.New("该红包已领取"), 112, "invitePacketGet:该红包已领取")
		return
	}

	// 提现0.3元
	// openId获取unionId
	unionId, err22 := model.UnionIdByUniqueId(inviteD.OpenId)
	utils.CheckErr(write, err22, 122, "invitePacketGet:无法获取用户信息")
	if err22 != nil {
		return
	}

	if unionId == "" {
		utils.CheckErr(write, errors.New("unionId获取失败, 请重新授权"), 110, "invitePacketGet:请点击开始游戏同意授权")
		return
	}

	// 请求php提现
	resp, err44 := http.Get("http://gc-api.ledongtian.com/gamecenter/public/api/drawGameActWGFK2?openId=" + inviteD.OpenId + "&unionId=" + unionId + "&gameid=1&id=" + strconv.Itoa(inviteD.Id))
	utils.CheckErr(write, err44, 144, "invitePacketGet:无法获取接口信息")
	if err44 != nil {
		return
	}

	defer resp.Body.Close()
	body, err55 := ioutil.ReadAll(resp.Body)
	utils.CheckErr(write, err55, 155, "invitePacketGet:无法获取接口data信息")
	if err55 != nil {
		return
	}

	response := drawRedPacketResponse{}
	err66 := json.Unmarshal(body, &response)
	utils.CheckErr(write, err66, 166, "invitePacketGet:无法解析body信息")
	if err66 != nil {
		return
	}

	// 判断是否提现成功
	if response.Code != 0 {
		utils.CheckErr(write, errors.New(response.Msg), 110, response.Msg)
		return
	} else {
		returnDa := utils.ReturnData{
			Errcode: 0,
			Errmsg:  response.Msg,
		}
		returnDa.Returnfunc(write)
		return
	}
}*/
